package srzy.common

import java.io.File
import java.lang.reflect.Field
import java.io.BufferedReader
import java.io.InputStreamReader
import java.io.FileInputStream
import scala.io.Source

object Common {

  val isWindows = System.getProperty("os.name").matches("Windows[a-zA-Z0-9 ]*")

  def getSysTime(format: String) = {
    val now = java.util.Calendar.getInstance(java.util.Locale.CHINA)
    val time = now.getTimeInMillis()
    val formater = new java.text.SimpleDateFormat(format)
    formater.format(time)
  }

  def readFromFile(fileName: String): List[String] = {
    val f = Source.fromFile(fileName, "GBK")
    val l = f.getLines.toList
    f.close
    l
  }

  def readFromFile(file: java.io.File): List[String] = {
    val f = Source.fromFile(file, "GBK")
    val l = f.getLines.toList
    f.close
    l
  }
  def writeToFile(filePath: String, s: String) {
    val f = new File(filePath)
    if (!f.exists()) {
      val d = f.getParentFile()
      if (!d.exists()) {
        d.mkdirs()
      }
      f.createNewFile()
    }
    val pw = new java.io.PrintWriter(f, "GBK")
    try {
      pw.write(s)
      pw.flush()
    } finally {
      pw.close
    }
  }

  def writeToFile(file: java.io.File, s: String) {
    val f = file
    if (!f.exists()) {
      val d = f.getParentFile()
      if (!d.exists()) {
        d.mkdirs()
      }
      f.createNewFile()
    }
    val pw = new java.io.PrintWriter(f, "GBK")
    try {
      pw.write(s)
      pw.flush()
    } finally {
      pw.close
    }
  }
  //loan pattern
  //  def writeToFile(filePath: String, s: String) {
  //    val f = new File(filePath)
  //    if (!f.exists())
  //      f.createNewFile()
  //    val pw = new java.io.PrintWriter(f, "UTF-8")
  //    try {
  //      pw.write(s)
  //    } finally {
  //      pw.close
  //    }
  //  }
  //
  //  def readFromFile(fileName: String): List[String] = {
  //    var ol = collection.mutable.ListBuffer[String]()
  //    var buf: BufferedReader = null
  //    try {
  //      buf = new BufferedReader(new InputStreamReader(new FileInputStream(fileName), "GBK"))
  //      var line: String = buf.readLine()
  //      while (line != null) {
  //        ol += (line)
  //        line = buf.readLine()
  //      }
  //    } finally {
  //      try {
  //        if (buf != null) {
  //          buf.close()
  //        }
  //      }
  //    }
  //    ol.toList
  //  }
  //  

  def getOrException[T](o: Option[T], s: String = "value"): T =
    o match {
      case Some(v) => v
      case None => throw new Exception(s + " is not assigned")
    }

  def ifnull(s: String) = if (s == null) "" else s

  def beanMap(o: Any) = {
    (o.getClass.getDeclaredFields.map(_.getName).toList.filter((t: String) => {
      """[^\w]""".r findFirstIn t match {
        case None => true
        case _ => false
      }
    }) //.filter(x => x matches "[A-Z0-9_]*")
      .map(x => (x, ((x: Field) => {
        x.setAccessible(true)
        x.get(o) match {
          case l: List[Any] => l.mkString("\n")
          case s: String => s
          case o: Any => o.toString
        }
      })(o.getClass.getDeclaredField(x))))).toMap
  }
}